package uk.ac.aber.paws.server.ownership;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import uk.ac.aber.paws.server.config.*;
import uk.ac.aber.paws.server.core.PAWSServer;
import uk.ac.aber.paws.server.db.DBConnectionObject;
import uk.ac.aber.paws.server.db.ParentDBOps;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 21st May 2012
 * 
 * AberUniOwnership.java
 * 
 * This class has been implemented specifically for Aberystwyth Uuniversity in order to 
 * query our in-house system for computer/owner registration information.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class AberUniOwnership extends ParentDBOps implements OwnershipQueryInterface {
	
	/**
	 * Returns the MAC address for the given computerName
	 */
	public String getMACAddress(String fqdn) {
		
		String returnVal = null;
		
		try {
			connectDB(new DBConnectionObject(Constants.MYSQLDB,ConfigManager.getProperty("app.db.ownership.principal"),null,ConfigManager.getProperty("app.db.ownership.name"),ConfigManager.getProperty("app.db.ownership.username"),ConfigManager.getProperty("app.db.ownership.password"),Integer.parseInt(ConfigManager.getProperty("app.db.ownership.port"))));

			Statement s = conn.createStatement();

			s.executeQuery("select tidymac(MAC),IP from "+ConfigManager.getProperty("app.db.ownership.table")+" where EquipName = '"+fqdn+"'");
			
			ResultSet rs = s.getResultSet();

			while (rs.next()) {
				returnVal = rs.getString("tidymac(MAC)");
			}
						
			rs.close();
			s.close();
		}
		catch (Exception ex) {
			PAWSServer.LOG_FILE.log(Level.ALL,ex.toString());
		}
		finally {
			disconnectDB();
		}
		
		return returnVal;
	}
	
	/**
	 * Returns the IP address for the giver computer name
	 */
	public String getIPAddress(String fqdn) {
		
		String returnVal = null;
		
		try
		{
			connectDB(new DBConnectionObject(Constants.MYSQLDB,ConfigManager.getProperty("app.db.ownership.principal"),null,ConfigManager.getProperty("app.db.ownership.name"),ConfigManager.getProperty("app.db.ownership.username"),ConfigManager.getProperty("app.db.ownership.password"),Integer.parseInt(ConfigManager.getProperty("app.db.ownership.port"))));

			Statement s = conn.createStatement();

			s.executeQuery("select tidymac(MAC),IP from "+ConfigManager.getProperty("app.db.ownership.table")+" where EquipName = '"+fqdn+"'");
			ResultSet rs = s.getResultSet();

			while (rs.next()) {
				returnVal = rs.getString("IP");
			}
			
			rs.close();
			s.close();
		}
		catch (Exception ex) {
			System.out.println(ex.toString());
		}
		finally {
			disconnectDB();
		}
		
		return returnVal;
	}
	
	/**
	 * Returns a list of computers registered as being owned by the given username. 
	 */
	public ArrayList<HashMap<String,String>> getMachineOwnership(String username) {
		
		ArrayList<HashMap<String,String>> returnVal = new ArrayList<HashMap<String,String>>();
		
		try{
			
			connectDB(new DBConnectionObject(Constants.MYSQLDB,ConfigManager.getProperty("app.db.ownership.principal"),null,ConfigManager.getProperty("app.db.ownership.name"),ConfigManager.getProperty("app.db.ownership.username"),ConfigManager.getProperty("app.db.ownership.password"),Integer.parseInt(ConfigManager.getProperty("app.db.ownership.port"))));

			Statement s = conn.createStatement();

			s.executeQuery("select * from "+ConfigManager.getProperty("app.db.ownership.table")+" where UserEmail = '"+username+"'");
			
			ResultSet rs = s.getResultSet();

			while (rs.next()) {
				
				HashMap<String,String> thisEntry = new HashMap<String,String>();
				
				thisEntry.put(OwnershipQueryInterface.COMPUTERNAME,rs.getString("EquipName").toUpperCase());
				thisEntry.put(OwnershipQueryInterface.IPADDRESS,rs.getString("IP"));
				thisEntry.put(OwnershipQueryInterface.MACADDRESS,rs.getString("MAC").toUpperCase());
				thisEntry.put(OwnershipQueryInterface.NOTES,rs.getString("Notes"));
				
				returnVal.add(thisEntry);
			}
						
			rs.close();
			s.close();
		}
		catch (Exception ex) {
			System.out.println(ex.toString());
		}
		finally {
			disconnectDB();
		}
		
		return returnVal;

	}
}
